package com.ymatou.iapi.productquery.testcase;

import com.mongodb.BasicDBObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productquery.parameter.GetNewProductListBySellerIdListBean;
import com.ymatou.iapi.productquery.parameter.NewProductIdDto;
import com.ymatou.iapi.productquery.service.GetNewProductListBySellerIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
*根据买手Id列表获取新品列表
*FreeMaker Template自动生成代码
*/
/**
 * 增加了缓存
 * @author zhoulei
 * @2018/09/10
 */
@TestSuite
public class Ts_GetNewProductListBySellerIdList{
	private static GetNewProductListBySellerIdListBean getnewproductlistbyselleridlistBean;
	private static GetNewProductListBySellerIdListCall getnewproductlistbyselleridlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买手Id列表获取新品列表");
	}

	@Before
	public void caseUp() {
		getnewproductlistbyselleridlistBean=new GetNewProductListBySellerIdListBean();
		getnewproductlistbyselleridlistCall=new GetNewProductListBySellerIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getnewproductlistbyselleridlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNewProductListBySellerIdList_001() {
		Logger.start(true,"根据买手Id列表获取新品列表");
		try {
			getnewproductlistbyselleridlistBean.setCount(10);
			getnewproductlistbyselleridlistBean.setSellerIds(Arrays.asList(3383,288484));
			getnewproductlistbyselleridlistCall.setData(getnewproductlistbyselleridlistBean);
			getnewproductlistbyselleridlistCall.callService();		
			Logger.verifyEquals("200", getnewproductlistbyselleridlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals("200", getnewproductlistbyselleridlistCall.getString("BCode"), "验证Bcode");
			//verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNewProductListBySellerIdList_002() {
		Logger.start(true,"根据买手Id列表获取新品列表,sellerId为空");
		try {
			getnewproductlistbyselleridlistBean.setCount(10);
			getnewproductlistbyselleridlistCall.setData(getnewproductlistbyselleridlistBean);
			getnewproductlistbyselleridlistCall.callService();			
			Logger.verifyEquals("201", getnewproductlistbyselleridlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-2", getnewproductlistbyselleridlistCall.getString("BCode"), "验证Bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	   /**快速创建商品 报错“商品不存在或已下架”，
		原因是直播调用商品服务先创建商品，同步商品id到同步mongo，然后创建成功的商品与直播id绑定，
		但是还没有等同步mongo完成就绑定了id，就出现商品不存在或已下架,这块业务直播绑定商品代码设计可能有问题，待开发修改
	*/
	public void Tc_GetNewProductListBySellerIdList_003() {
		Logger.start(true,"根据买手Id列表获取新品列表,排除直播快速发布商品");
		try {
			int sellerid = 288484;
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerid));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());

			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerid);
			addProdReq.getProduct().setLiveRoomId(createlive.getId());
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);

			getnewproductlistbyselleridlistBean.setSellerIds(Arrays.asList(sellerid));
			getnewproductlistbyselleridlistBean.setCount(10);
			getnewproductlistbyselleridlistCall.setData(getnewproductlistbyselleridlistBean);
			getnewproductlistbyselleridlistCall.callService();
			Logger.verifyEquals("200", getnewproductlistbyselleridlistCall.getString("Code"), "验证Code");
			Logger.verifyEquals("200", getnewproductlistbyselleridlistCall.getString("BCode"), "验证Bcode");
			List<NewProductIdDto> productList = getnewproductlistbyselleridlistCall.getProductList();
			productList.forEach(p->{
				if (p.getSellerId()==sellerid){
					Logger.verifyEquals(false,p.getProductIds().toString().contains(productId),"验证返回数据没有直播发布商品");
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public void verifyDB() throws Exception{
		List<NewProductIdDto> productList = getnewproductlistbyselleridlistCall.getProductList();
		ProductsWapper ProductsWapper = new ProductsWapper();
		if(productList != null && productList.size() != 0){
			for (int i = 0;i < productList.size();i++) {
				NewProductIdDto newProductIdDto = productList.get(i);
				BasicDBObject search = new BasicDBObject();
				search.append("sid", getnewproductlistbyselleridlistBean.getSellerIds().get(i));
				search.append("isnew", true);
				BasicDBObject orderBy = new BasicDBObject("_id", -1);
				List<Map> dlist = ProductsWapper.selectProduct(search, orderBy);
				Logger.debug(dlist);
				for(int j=0;j<newProductIdDto.getProductIds().size();j++){

				}
			}
		}
		
		Logger.debug(productList);
	}
}